package com.sqx.modules.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.AppProduct;
import com.sqx.modules.health.service.IAppProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "产品相关接口", tags = "产品相关接口", description = "")
@RestController
@RequestMapping("/health/appProduct")
public class AppProductController {
        @Autowired
        private IAppProductService appProductService;

        // 新增或更新
        @PostMapping("/save")
        @ApiOperation(value = "新增或更新", notes = "新增或更新")
        public Result save(@RequestBody AppProduct appProduct){
            return Result.success(appProductService.save(appProduct));
        }

        // 根据ID删除
        @DeleteMapping("/del/{id}")
        @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
        public Result delete(@PathVariable Integer id){
            return Result.success(appProductService.removeById(id));
        }

        // 根据IDS删除
        @PostMapping("/del/batch")
        @ApiOperation(value = "根据IDS删除", notes = "根据IDS删除")
        public Result deleteBatch(@RequestBody List<Integer> ids){
            return Result.success(appProductService.removeByIds(ids));
        }

        // 查询全部
        @GetMapping("/findAll")
        @ApiOperation(value = "查询全部", notes = "查询全部")
        public Result findAll(){
            return Result.success(appProductService.list());
        }

        // 根据ID查询一条记录
        @GetMapping("/findOne")
        @ApiOperation(value = "根据ID查询一条记录", notes = "根据ID查询一条记录")
        public Result findOne(@PathVariable Integer id){
            return Result.success(appProductService.getById(id));
        }

        // 分页查询
        @GetMapping("/findPage")
        @ApiOperation(value = "分页查询", notes = "分页查询")
        public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize){

            QueryWrapper<AppProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("id");

            Page<AppProduct> result = appProductService.page(new Page<>(pageNum, pageSize), queryWrapper);

            return  Result.success(result);
        }

    }

